package com.example.ysm.recyclerviewdata_416ysm.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.ysm.recyclerviewdata_416ysm.adapter.TcareAdapter;
import com.example.ysm.recyclerviewdata_416ysm.bean.ArticleBean;
import com.example.ysm.recyclerviewdata_416ysm.adapter.MyAdapter;
import com.example.ysm.recyclerviewdata_416ysm.R;
import com.example.ysm.recyclerviewdata_416ysm.bean.ArticleBean;
import com.example.ysm.recyclerviewdata_416ysm.iface.ArticleListener;
import com.example.ysm.recyclerviewdata_416ysm.iface.TcareListener;
import com.example.ysm.recyclerviewdata_416ysm.model.ArticleModel;
import com.example.ysm.recyclerviewdata_416ysm.model.TcareModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
  private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;//显示布局效果
    private MyAdapter tcareAdapter;//适配器
    private List<ArticleBean> list=null;//数据源
    ArticleListener tcareListener=new ArticleListener() {

        public void onResponse(List<ArticleBean> beanlist) {
            list=beanlist;
            tcareAdapter.setList(list);//得到网书中的数据 传到adapter
            //通知的话 数据更新
            tcareAdapter.notifyDataSetChanged();
        }


        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this, "msg", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       ArticleModel tcareModel=new ArticleModel();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();


        //调用哪个方法 拿到数据
        //第二页
        //在这使用它 实例化Artivitymodel 调用方法获取网络数据

        tcareModel.getResoutList("article",2,"rv1l2tidbomann2qn86nln6ur2", tcareListener);


    }

    private void initRecyclerView() {
        //获取RecyclerView 设置属性 获取数据源 绑定
        recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
        //创建默认的线性布局
        layoutManager=new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
      recyclerView.setHasFixedSize(true);
      //  list=getData();
        tcareAdapter=new MyAdapter(this);
        tcareAdapter.setList(list);
        recyclerView.setAdapter(tcareAdapter);
    }
/*
    //自定义： 获取数据源并形成list
    private List<ArticleBean> getData(){
        //定义List 赋值 返回
        List<ArticleBean> list=new ArrayList<>();
        ArticleBean articleBean;
        //第一组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("标题1");
        articleBean.setDescription("描述1");
        articleBean.setTime("2017-4-1");
        articleBean.setImgid(R.drawable.f1);
        list.add(articleBean);
        //第二组
        articleBean=new ArticleBean();
        articleBean.setArticleId(2);
        articleBean.setTitle("标题2");
        articleBean.setDescription("描述2");
        articleBean.setTime("2017-4-2");
        articleBean.setImgid(R.drawable.f2);
        list.add(articleBean);
        //第三组
        articleBean=new ArticleBean();
        articleBean.setArticleId(3);
        articleBean.setTitle("标题3");
        articleBean.setDescription("描述3");
        articleBean.setTime("2017-4-3");
        articleBean.setImgid(R.drawable.f3);
        list.add(articleBean);
        //第四组
        articleBean=new ArticleBean();
        articleBean.setArticleId(4);
        articleBean.setTitle("标题4");
        articleBean.setDescription("描述4");
        articleBean.setTime("2017-4-4");
        articleBean.setImgid(R.drawable.f4);
        list.add(articleBean);
        //第五组
        articleBean=new ArticleBean();
        articleBean.setArticleId(5);
        articleBean.setTitle("标题5");
        articleBean.setDescription("描述5");
        articleBean.setTime("2017-4-5");
        articleBean.setImgid(R.drawable.f5);
        list.add(articleBean);
        //第六组
        articleBean=new ArticleBean();
        articleBean.setArticleId(6);
        articleBean.setTitle("标题6");
        articleBean.setDescription("描述6");
        articleBean.setTime("2017-4-6");
        articleBean.setImgid(R.drawable.f6);
        list.add(articleBean);
        return list;
    }*/
}
